//
//  PywDeleteAccountApi.m
//  PywSdk
//
//  Created by smallwave on 16/8/29.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywDeleteAccountApi.h"
#import "AIFDeleteAccountManager.h"
#import "PropertyListReformer.h"
#import "PywMBProgressHUD+Pyw.h"

@interface PywDeleteAccountApi ()<ETAPIManagerCallBackDelegate>

@end

@implementation PywDeleteAccountApi

- (void)requestDeleteAccountWithAccount:(NSString *)account
                                Success:(Successd)success
                                   fail:(Failure)fail{
    self.successd = success;
    self.failure = fail;
    
    AIFDeleteAccountManager *manager = [AIFDeleteAccountManager manager];
    manager.account = account;
    manager.delegate = self;
    [manager loadData];
}
#pragma mark - ETAPIManagerCallBackDelegate
- (void)managerCallAPIDidSuccess:(ETApiBaseManager *)manager{
    PropertyListReformer *reformer = [[PropertyListReformer alloc] init];
    NSDictionary *dict = [manager fetchDataWithReformer:reformer];
    
    [self deleteAccountWithDict:dict];
}
- (void)managerCallApiDidFailed:(ETApiBaseManager *)manager{
    [PywMBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow];
    [PywMBProgressHUD showError:@"删除游戏账号失败！" toView:[UIApplication sharedApplication].keyWindow];
    
    [self deleteAccountFail];
}
/**
 *  删除成功
 */
- (void)deleteAccountWithDict:(NSDictionary *)dict{
    int resultCode = [[dict objectForKey:@"ack"] intValue];
    [PywMBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow];
    
    switch (resultCode) {
        case 200:
        {
            if (self.successd) {
                self.successd(nil,nil);
            }
        }
            break;
            
        default:
        {
            NSString *errorMsg = [NSString stringWithFormat:@"%@",dict[@"msg"]];
            [PywMBProgressHUD showError:errorMsg toView:[UIApplication sharedApplication].keyWindow];
            if (self.failure) {
                self.failure([self errorWithCode:resultCode]);
            }
        }
            break;
    }}
/**
 *  删除失败
 */
- (void)deleteAccountFail{
    if (self.failure) {
        self.failure([self errorWithCode:0]);
    }
}

@end
